package Random;

import java.util.ArrayList;
import java.util.Arrays; 
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random; 
import java.util.Set;

/** 
* 其实思路很简单，就是从seed数组中取出还未选中的种子 
*/ 
public class RandomTest { 
	//@ 用HashSet存放无重复的数据
	public void getNoRepeatRandom(){
	Set<Integer> set = new HashSet<Integer>();
	while (true) {
           if (set.size() >= 50)
                   break;
           set.add(new Random().nextInt(1000));
		   }
		   Iterator it= set.iterator();
		   while(it.hasNext()){
			   System.out.print(it.next() + "  ");  
		   }
		  /* for (int n : set) {
		            System.out.print(n + "  ");
		   }*/
      } 
	
	//@效率最低的一种 
	public void getNoRepeatRandom(int number){
	
	  int[] intRandom = new int[number];
      List mylist = new ArrayList();  //生成数据集，用来保存随即生成数，并用于判断
      Random rd = new Random();
      while(mylist.size() < number) {
          int num = rd.nextInt(number+1);
        
          if(!mylist.contains(num)) {  
             mylist.add(num);  //往集合里面添加数据。
          }
      }
    
     for(int i = 0;i <mylist.size();i++) {
          intRandom[i] = (Integer)(mylist.get(i));
     }
   }
	public static void main(String[] args) { 

		new RandomTest().getNoRepeatRandom();
		
	}
	
	 
}
